package main

import (
	"fmt"
)

func main() {
	fmt.Println(maxAltitude([]int{3, 2, 1, -1, -2, -3, -4}, 3))
}

func maxAltitude(heights []int, limit int) []int {
	queue := []int{}
	res := []int{}
	for i, nums := range heights {
		for len(queue) > 0 && queue[len(queue)-1] < nums {
			queue = queue[:len(queue)-1]
		}
		queue = append(queue, nums)

		if i > limit-1 && queue[0] == heights[i-limit] {
			queue = queue[1:]
		}

		if i >= limit-1 {
			res = append(res, queue[0])
		}
	}
	return res
}
